package de.koller.gravity.mech.particle.environment;

import de.koller.aframe.util.math.FunctionPreview;
import de.koller.aframe.util.math.IFunction;
import de.koller.gravity.mech.particle.Particle;
import de.koller.gravity.mech.particle.ParticleEngine;
import de.koller.oceanFramework.util.ColorIntUtil;

public class FadingParticles implements IParticleEnvironment {

	private IFunction alphafade;

	public FadingParticles( IFunction alphafade ) {
		this.alphafade = alphafade;
	}
	
	@Override
	public void stepParticle( Particle p, ParticleEngine engine ) {
		float lt = engine.getEngineStep() - p.creationTime;
		p.color = ColorIntUtil.getIntColor( (int)alphafade.getValue( lt ), 
											ColorIntUtil.getRValue( p.color ),
											ColorIntUtil.getGValue( p.color ),
											ColorIntUtil.getBValue( p.color ) );
	}
	
	public static IFunction createLinearFade( long timeToFade ) {
		return new LinearFade( timeToFade );
	}
	
	private static class LinearFade implements IFunction {
		private final long fadetime;

		public LinearFade( long fadetime ) {
			this.fadetime = fadetime;
		}

		@Override
		public float getValue( float x ) {
			float c = -(float)0xff / (float)fadetime * x + 0xff;
			if( c > 0xff ) c = 0xff;
			if( c < 0 ) c = 0;
			return c;
		}
	
	}

	public static void main( String[] args ) {
		FunctionPreview.showFunction( -100, 2100, FadingParticles.createLinearFade( 2000 ) );
	}
}
